package com.qzsoft.system.modules.permission.roleclass.controller;

import com.qzsoft.jeemis.common.annotation.LogOperation;
import com.qzsoft.jeemis.common.utils.ConvertUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.AssertUtils;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.DefaultGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;

import com.qzsoft.system.common.PinYinUtils;
import com.qzsoft.system.modules.permission.role.dto.SysRoleDTO;
import com.qzsoft.system.modules.permission.roleclass.dto.SysRoleClassDTO;
import com.qzsoft.system.modules.permission.roleclass.dto.SysRoleClassTreeDTO;
import com.qzsoft.system.modules.permission.roleclass.entity.SysRoleClassEntity;
import com.qzsoft.system.modules.permission.roleclass.service.SysRoleClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 权限角色分组表(权限角色)(SysRoleGroup)表控制层
 *
 * @author ygq
 * @since 2020-09-22 14:24:44
 */
@RestController
@RequestMapping("/sys/sys_role_class")
@Api(tags = "权限角色分组表(权限角色)")
@Log4j2
public class SysRoleClassController {
    @Autowired
    private SysRoleClassService sysRoleGroupService;

    @GetMapping("publicTree")
    @ApiOperation("通用角色分组树")
    public Result<List<SysRoleClassTreeDTO>> publicTree() {
        List<SysRoleClassTreeDTO> data = sysRoleGroupService.treeFolder();
        return new Result<List<SysRoleClassTreeDTO>>().ok(data);
    }

    @GetMapping("tree_role")
    @ApiOperation("分组角色树")
    public Result<List<SysRoleClassTreeDTO>> treeRole() {
        List<SysRoleClassTreeDTO> data = sysRoleGroupService.treeRoleList();
        return new Result<List<SysRoleClassTreeDTO>>().ok(data);
    }

    @GetMapping("mix_tree_role")
    @ApiOperation("角色_组织_组的角色树")
    public Result<List<SysRoleClassTreeDTO>> getTreeRole(@RequestParam("deptids") List<String> deptids,@RequestParam("groupids") List<String> groupids) {
        System.out.println(deptids);
        List<SysRoleClassTreeDTO> data = sysRoleGroupService.getTreeRoleList(deptids,groupids);
        return new Result<List<SysRoleClassTreeDTO>>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存角色分组")
    public Result save(@Valid @RequestBody SysRoleClassDTO dto) {
        SysRoleClassEntity data = ConvertUtils.sourceToTarget(dto, SysRoleClassEntity.class);

        return new Result().ok(sysRoleGroupService.save(data));
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("sys:role:update")
    public Result update(@RequestBody SysRoleClassDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysRoleGroupService.update(dto);

        return new Result();
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SysRoleClassEntity> get(@PathVariable("id") String id) {
        SysRoleClassEntity data = sysRoleGroupService.get(id);

        return new Result<SysRoleClassEntity>().ok(data);
    }

    @GetMapping("getfullpinyin")
    @ApiOperation("列表")
    public String getfullpinyin(String hanzi) {
        AssertUtils.isBlank(hanzi, "汉字");
        return PinYinUtils.getFullPinYin(hanzi);
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody String[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysRoleGroupService.delete(ids);

        return new Result();
    }

    @PostMapping("get_dept_default_role")
    @ApiOperation("获取部门默认的角色")
    @LogOperation("获取部门默认的角色")
    public Result<SysRoleDTO> getDeptDefaultRole(String id)  {
        SysRoleDTO sysRoleV2DTO = sysRoleGroupService.getDeptDefaultRole(id);
        return new Result<SysRoleDTO>().ok(sysRoleV2DTO);
    }
}